﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GOAP.Entities;

namespace GOAP
{
    class World
    {
        protected Dictionary<string, Entity> entityCollection;
        protected Dictionary<string, string> goapData;
        protected int entityCounter = 0;

        public World()
        {
            entityCollection = new Dictionary<string, Entity>();
            goapData = new Dictionary<string, string>();
        }

        public T createEntity<T>(string name) where T : Entity
        {
            T ent = Activator.CreateInstance<T>();
            ent.name = name + entityCounter; //Ensure unique names

            entityCounter++;
            return ent;
        }

        public void addEntity(Entity ent)
        {
            entityCollection.Add(ent.name, ent);
            ent.world = this;
        }

        public T getEntity<T>(string entName) where T: Entity
        {
            if (entityCollection.ContainsKey(entName))
                return entityCollection[entName] as T;
            return null;
        }

        public void printEntities()
        {
            foreach (KeyValuePair<string, Entity> pair in entityCollection)
            {
                pair.Value.print();
            }
        }

        public World clone()
        {
            World copy = new World();

            //Clone goapData into new world
            copy.goapData = new Dictionary<string, string>(goapData);

            //Clone entities
            copy.entityCollection = new Dictionary<string, Entity>();
            foreach (KeyValuePair<string, Entity> pair in entityCollection)
            {
                Entity entcopy = pair.Value.clone();
                copy.addEntity(entcopy); //entcopy so the world parameter is set correctly
            }

            //Copy counter to keep naming unique
            copy.entityCounter = entityCounter;

            return copy;
        }
    }
}
